<template>
  <div id="person_detail">
    <h2>个人主页</h2>
    <unit-title title="信息修改" :mores="false"></unit-title>
<!--    用户信息修改-->
    <div class="person_update">
      <el-form ref="form" :model="newUserObj" label-width="80px" label-position="top">
        <el-form-item label="更新头像">
          <el-upload
              class="avatar-uploader"
              :action="`${baseurl}/api/image/uploadImage`"
              :show-file-list="false"
              :on-success="handleAvatarSuccess">
            <img v-if="imgUrl" :src="newUserObj.avatar?newUserObj.avatar:imgUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="newUserObj.password"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码">
          <el-input v-model="newUserObj.againPassword"></el-input>
        </el-form-item>
        <el-button type="primary" @click="updateUser">确定更新</el-button>
      </el-form>
    </div>

<!--    用户借阅信息-->
    <unit-title title="图书借阅信息" :mores="false" style="margin-top: 50px"></unit-title>
    <el-table
        :data="borrowList"
        style="width: 100%">
      <el-table-column
          prop="name"
          label="书籍名称">
      </el-table-column>
      <el-table-column
          prop="author"
          label="图书作者">
      </el-table-column>
      <el-table-column label="图书封面">
        <template slot-scope="scope">
          <img class="book_cover" :src="scope.row.cover">
        </template>
      </el-table-column>
      <el-table-column
          prop="gmtCreate"
          label="借阅时间"
          width="180">
      </el-table-column>
    </el-table>

    <div class="home_footer">
      | 版本所有权：王璐佳 |
    </div>
  </div>
</template>

<script>
import baseurl from "../const/baseurl";
import UnitTitle from "../components/Home/UnitTitle";
import {updateUser,getUserBorrows} from "../network/homeRequest";
export default {
  name: "PersonalDetail",
  components: {UnitTitle},
  data(){
    return{
      baseurl,
      newUserObj:{},
      imgUrl:"",
      borrowList:[],
    }
  },
  created() {
    this.newUserObj.password=sessionStorage.getItem("wlj-user-password");
    this.imgUrl=sessionStorage.getItem("wlj-user-image");
    this.newUserObj.id=sessionStorage.getItem("wlj-user-id");
    //获取当前用户的借阅信息
    getUserBorrows(this.newUserObj.id).then((result)=>{
      console.log(result);
      this.borrowList=result.data.data;
    }).catch((reason)=>{
      console.log(reason);
    })
  },
  methods:{
    updateUser(){
      console.log(this.newUserObj);
      if (this.newUserObj.password===this.newUserObj.againPassword) {
        updateUser(this.newUserObj).then((result) => {
          console.log(result);
          if (result.data.code === 200) {
            this.$message({
              message: '更新成功!',
              type: 'success'
            });
            sessionStorage.setItem("wlj-url", "/persondetail");
            this.$router.push("/blankpage");
          } else {
            this.$message({
              message: '更新失败!',
              type: 'warning'
            });
          }
        })
      }else{
        this.$message({
          message: '两次密码不相同!',
          type: 'warning'
        });
      }
    },
    handleAvatarSuccess(res){
      console.log(res)
      this.newUserObj.avatar=res.data;
    }
  }
}
</script>

<style scoped>
#person_detail{
  width: 1200px;
  margin: 0 auto;
}
#person_detail h2{
  text-align: center;
  line-height: 60px;
}
.person_update{
  width: 50%;
  margin: 20px auto;
}
/*图书封面*/
.book_cover{
  width: 150px;
}


.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

/*底部*/
.home_footer{
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  background: #eee;
}
</style>
